<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>

</html>
<script>
    //type,url,data,async,success,error
    // let xhr = new XMLHttpRequest();
    // xhr.open('get', 'http://api.k780.com/?app=weather.future&weaId=1&&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json%27', true);
    // xhr.send();
    // xhr.onload = function() {
    //     if (xhr.readyState === 4) {
    //         console.log(JSON.parse(xhr.responseText));
    //     }
    // }

    function objtostring(obj) {
        if (Object.prototype.toString.call(obj) === '[object Object]') {
            let arr = [];
            for (let attr in obj) {
                arr.push(attr + "=" + obj[attr]);
            }
        }
        return arr.join("&")
    }

    function $ajax(option) {
        let xhr = new XMLHttpRequest();
        option.type = option.type || 'get';
        if (!option.url) {
            throw new Error("接口地址不能为空");
        }
        if (option.async === false || option.async === "flase") {
            option.async = false
        } else {
            option.async = true
        }
        if (option.data && option.type === "get") {
            option.url += '?' + objtostring(option.data)
        }
        xhr.open(option.type, option.url, option.async)
    }
</script>